package cn.hesion.leetcode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class MinimumOperations {
    public int minimumOperations(int[] nums, int start, int goal) {
        int maxn=1010;
        LinkedList<Integer> q=new LinkedList<Integer>();
        int[] step = new int[maxn];                                                                     

        for(int a=0;a<maxn;++a) step[a]=-1;
        int x,nextx,i;
        q.push(start);
        step[start]=0;

        while(!q.isEmpty()){
            x=q.peekFirst();
            q.pop();
            for (i = 0; i < nums.length; ++i) {
                nextx = x+nums[i];
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }

                nextx = x-nums[i];
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }

                nextx = (x^nums[i]);
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int i = new MinimumOperations().minimumOperations(new int[]{2, 4, 12}, 2, 12);
        System.out.println(i);
    }
}
